<?php

namespace app\api\validate;

use app\api\model\User as UserModel;
use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
	    'phone'=>'require|number',
	    'country'=>'require|number',
	    'phone_code'=>'require|number',
	    'user_id'=>'require|number',
        'code'=>'require|number',
        'repassword'=>'confirm:password',
        'password'=>'compare',
        'type'=>'require|in:1,2',
        'username'=>'require|max:30',
        'email'=>'require|email|checkEmail',
        'avatar'=>'require',

    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'phone.require'=>'Phone cannot be empty',
        'phone_code.require'=>'Phone code cannot be empty',
        'country.require'=>'Country cannot be empty',
        'code.require'=>'Code cannot be empty',
        'user_id.require'=>'UserID cannot be empty',
        'type.require'=>'Type cannot be empty',
        'password.require'=>'Password cannot be empty',
        'avatar.require'=>'Avatar cannot be empty',
        'email.require'=>'Email cannot be empty',
        'phone.number'=>'Phone must be a number',
        'phone_code.number'=>'Phone code must be a number',
        'user_id.number'=>'UserID must be a number',
        'code.number'=>'Code must be a number',
        'country.number'=>'Country must be a number',
        'type.in'=>'Type must be 1 or 2',
        'repassword.confirm'=>'Inconsistent passwords entered twice',
        'username.require'=>'Username cannot be empty',
        'username:max'=>'Username cannot exceed 30 characters in length',
        'email.email'=>'Incorrect email format'
    ];

    protected $scene=[
        'complete'=>['password','repassword','email','username'],
        'edit'=>['username'],
        'avatar'=>['avatar']
    ];

    protected function compare($value,$rule,$data=[])
    {
        if(isset($data['password']) && !isset($data['repassword'])){
            return 'Repassword cannot be empty';
        }

        //定义正则匹配规则
        $pattern = '/^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*]{8,}$/';
        //使用preg_match()函数进行匹配
        if (!preg_match($pattern, $data['password'])) {
            return 'The password must contain letters, numbers, and be at least 8 digits in length';
        }
        return true;
    }

    /**
     * 验证是否唯一
     * @param $value
     * @param $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkEmail($value,$rule,$data=[])
    {
        $count=UserModel::where(['email'=>$value])->where('status','>=',0)->count();
        if($count){
            return 'The current email is already bound and cannot be changed';
        }else{
            return true;
        }
    }


    public function sceneLogin_2(){
        return $this->only(['email','password','type'])
            ->remove('password','compare')
            ->remove('email','checkEmail')
            ->append('password', 'require');
    }

    public function sceneLogin_1(){
        return $this->only(['code','type','email'])
            ->remove('email','checkEmail')
            ->append('email', 'email')
            ->append('email', 'require');
    }

    public function scenelogoff(){
        return $this->only(['email','code'])
            ->remove('email','checkEmail')
            ->append('email', 'email')
            ->append('email', 'require');
    }
}
